{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4096,)\n",
      "[ 0.          0.          0.77542615 ...,  0.          0.          0.        ]\n"
     ]
    }
   ],
   "source": [
    "# Append the path to caffe/python to your PYTHONPATH\n",
    "import sys\n",
    "sys.path.append('/home/gavin/caffe/python')\n",
    "\n",
    "import os\n",
    "import caffe\n",
    "import numpy as np\n",
    "\n",
    "CAFFE_DIR = '/home/gavin/caffe'\n",
    "MEAN_PATH = os.path.join(CAFFE_DIR, 'python/caffe/imagenet/ilsvrc_2012_mean.npy')\n",
    "PROTOTXT_PATH = os.path.join(CAFFE_DIR, 'models/bvlc_reference_caffenet/deploy.prototxt')\n",
    "CAFFEMODEL_PATH = os.path.join(CAFFE_DIR, 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel')\n",
    "IMAGE_PATH = 'img/zipper-1.jpg'\n",
    "\n",
    "caffe.set_mode_cpu()\n",
    "net = caffe.Net(PROTOTXT_PATH, CAFFEMODEL_PATH, caffe.TEST)\n",
    "transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})\n",
    "transformer.set_transpose('data', (2, 0, 1))\n",
    "transformer.set_mean('data', np.load(MEAN_PATH).mean(1).mean(1))\n",
    "transformer.set_raw_scale('data', 255)\n",
    "transformer.set_channel_swap('data', (2,1,0))\n",
    "\n",
    "net.blobs['data'].reshape(1, 3, 227, 227)\n",
    "net.blobs['data'].data[0] = transformer.preprocess('data', caffe.io.load_image(IMAGE_PATH))\n",
    "net.forward()\n",
    "features = net.blobs['fc7'].data.reshape(-1,)\n",
    "print(features.shape)\n",
    "print(features)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
